package dailearn.config.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author daify
 * @date 2019-07-04 17:28
 **/
@Log
@Service
public class ClientService {

    @Autowired LoadBalancerClient loadBalancerClient;
    @Autowired RestTemplate restTemplate;

    /**
     * 配置降级方法
     * @return
     */
    @HystrixCommand(fallbackMethod = "getServiceBack")
    public String getService() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("base-producer");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/getService";
        log.info(url);
        return restTemplate.postForObject(url,null, String.class);
    }


    public String getServiceBack() {
        return "use fallbackMethod";
    }
    
}
